const regeneratorRuntime = require('../../../vendor/regenerator-runtime')
const co = require('../../../vendor/co')
let tid = 0
const util = require("../../../utils/util")
const app = getApp()


//根据秒数计算时分秒
function computeTime(val) {
    if (val < 0) {
        return ["00", "00", '00']
    }
    function format(d) {
        return d < 10 ? ("0" + d) : d
    }
    let h = parseInt(val / 3600)//时
    let _h = val - h * 3600//除去 小时 剩余的秒数
    let m = parseInt(_h / 60)//分
    let s = _h % 60//秒
    return [h, m, s]
}

let startAt = 0, endAt = 0
Page({
    data: {
        status: 80,
        time_left: ["00", "00", "00"]
    },
    update: function () {
        this.setthis.data(this.data)
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.setData(options)
    },

    onShow: function () {
        this.loadData()
    },


    loadData() {
        if (this.data.loading) {
            return
        }
        co(function* () {
            try {
                wx.showLoading({
                    title: '加载中...',
                })
                this.setData({
                    loading: true
                })
                //状态码
                const statusCode = yield app.http.get("order/status-dict")
                //团详情
                const result = yield app.http.get("group-order/view", {
                    data: { id: this.data.id }
                })
                const status = result.data.group.status
                const d = {
                    statusCode: statusCode.data,
                    targetNum: parseInt(result.data.group.need_number),
                    currentNum: result.data.group.children.length,
                    goods: result.data.group.goodsOrderItems,
                    status,
                    recommend: result.data.recommend,
                    price: (result.data.group.goodsOrderItems.price / 100).toFixed(2),
                    groupStatus: 1,
                    endAt: result.data.group.end_time * 1000,
                    loading: false
                }


                if (status == statusCode.data.STATU_GROUP_PURCHASE.code) {
                    d.groupStatus = 1
                    if (+result.data.group.end_time < +new Date() / 1000) {
                        d.groupStatus = 3
                    }
                }
                else if ([
                    statusCode.data.STATU_GROUP_FAILED.code,//组团失败
                    statusCode.data.STATU_REFUNDING.code,//退款中
                    statusCode.data.STATU_REFUND_FAILED.code,//退款失败
                    statusCode.data.STATU_REFUND_SUCCESS.code,//退款失败
                ].includes(parseInt(status))) {
                    d.groupStatus = 3
                } else {
                    d.groupStatus = 2
                }



                const { member_id, member_image, member_name } = result.data.group
                const ms = [
                    {
                        name: member_name,
                        image: member_image,
                        id: member_id
                    }
                ]
                ms.push(...result.data.group.children.map(o => {
                    return { name: o.member_name, image: o.member_image, id: o.member_id }
                }))
                // console.log(ms)
                // d.members = ms.slice(0, 3)
                // d.members=ms
                d.members = ms
                //计算开团时间
                d.startTime = util.formatTime(new Date(result.data.group.created_at * 1000))
                const mid = wx.getStorageSync("mid")
                if (ms.find(o => o.id == mid)) {
                    d.isJoin = true
                }
                this.setData(d)
                wx.hideLoading()
                wx.stopPullDownRefresh()
                app.checkShare(this.data)
                if (d.groupStatus == 1) {
                    this.doTimer()
                    this.startTimer()
                }
            } catch (err) {
                wx.stopPullDownRefresh()
                app.wxToast.error(err.message)
            }
        }.bind(this))

    },
    onPullDownRefresh() {
        this.loadData()
    },

    startTimer: function () {
        clearInterval(tid)
        tid = setInterval(() => {
            this.doTimer()
        }, 1000)
    },

    doTimer() {
        let t = this.data.endAt - parseInt(+new Date())
        if (t <= 0) {
            clearInterval(tid)
        }
        this.setData({
            time_left: computeTime(parseInt(t / 1000))
        })
    },
    hideMask: function () {
        this.update()
    },

    onShareAppMessage: function () {
        let sm = wx.getStorageSync('store')
        let info = {
            title: `我在【${sm}】上买了${this.data.goods.name}`,
            desc: "还差" + (this.data.targetNum - this.data.currentNum - 1) + "人，快来参团吧",
        }

        if (this.data.groupStatus != 1) {
            info.path = '/pages/goods/detail/detail?id=' + this.data.goods.goods_id
        } else {
            info.path = '/pages/pt/ok/ok?id=' + this.data.id
        }
        console.log(info)
        return info
    },
    join() {
        wx.navigateTo({
            url: `/pages/pay/pay?id=${this.data.goods.goods_id}&tid2=${this.data.id}&join=1`,
        })
    },
    kt() {
        wx.redirectTo({
            url: '/pages/goods/detail/detail?id=' + this.data.goods.goods_id,
        })
    }
})